package my.android.menutest;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;

public class MenuTest extends Activity {
	//MAIN MENU ITEMS
	private PopupWindow mainmenu;
	private Button mainmenu_btn_single, mainmenu_btn_multi, mainmenu_btn_quit, mainmenu_btn_options, mainmenu_btn_tutorial;
	private PopupWindow solomenu;
	private Button solomenu_btn_start, solomenu_btn_back;
	
	//PAUSE MENU ITEMS
	private PopupWindow pausemenu;
	private Button pausemenu_btn_resume, pausemenu_btn_mainmenu, pausemenu_btn_quit;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mainmenubg);
		
		
		createMainMenu();
		createSoloMenu();
		
		createPauseMenu();
		
		
	}

	@Override
	public void onWindowFocusChanged(boolean x)
	{
		
		super.onWindowFocusChanged(x);
		toggleWindow(mainmenu);
	}
	
	
	// CREATE MAIN MENU
	public void createMainMenu(){
		mainmenu = new PopupWindow(View.inflate(this, R.layout.mainmenu, null), LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, false);
	    //mainmenu.setAnimationStyle(R.style.mainmenu_anim);
	    
		//buttons here
		mainmenu_btn_single = (Button)mainmenu.getContentView().findViewById(R.id.mainmenu_btn_single);
	    mainmenu_btn_single.setOnClickListener(mainmenuListener);
		mainmenu_btn_multi = (Button)mainmenu.getContentView().findViewById(R.id.mainmenu_btn_multi);
		mainmenu_btn_multi.setOnClickListener(mainmenuListener);
	    mainmenu_btn_options = (Button)mainmenu.getContentView().findViewById(R.id.mainmenu_btn_options);
	    mainmenu_btn_options.setOnClickListener(mainmenuListener);
	    mainmenu_btn_tutorial = (Button)mainmenu.getContentView().findViewById(R.id.mainmenu_btn_tutorial);
	    mainmenu_btn_tutorial.setOnClickListener(mainmenuListener);
	    mainmenu_btn_quit = (Button)mainmenu.getContentView().findViewById(R.id.mainmenu_btn_quit);
	    mainmenu_btn_quit.setOnClickListener(mainmenuListener);
	}
	// CREATE SOLO SUBMENU
	public void createSoloMenu(){
		solomenu = new PopupWindow(View.inflate(this, R.layout.solomenu, null), LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, false);
	    //mainmenu.setAnimationStyle(R.style.mainmenu_anim);
	    
		//buttons here
		solomenu_btn_start = (Button)solomenu.getContentView().findViewById(R.id.solomenu_btn_start);
	    solomenu_btn_start.setOnClickListener(mainmenuListener);
		solomenu_btn_back = (Button)solomenu.getContentView().findViewById(R.id.solomenu_btn_back);
		solomenu_btn_back.setOnClickListener(mainmenuListener);
	}
	
	//MAIN MENU CLICK LISTENERS
	private OnClickListener mainmenuListener = new OnClickListener() {
	    public void onClick(View v) {
	    	if(v==mainmenu_btn_single){
	    		toggleWindow(mainmenu);
	    		toggleWindow(solomenu);
	    	}
	    	if(v==solomenu_btn_start){
	    		//toggleWindow(mainmenu);
	    		//toggleWindow(singlemenu);
	    	}
	    	if(v==solomenu_btn_back){
	    		toggleWindow(solomenu);
	    		toggleWindow(mainmenu);
	    	}
	    	
	    	
	    	if(v==mainmenu_btn_multi){
	    		toggleWindow(mainmenu);
	    		//toggleWindow(multimenu);
	    	}
	    	if(v==mainmenu_btn_options){
	    		toggleWindow(mainmenu);
	    		//toggleWindow(optionsmenu);
	    	}
	    	if(v==mainmenu_btn_tutorial){
	    		toggleWindow(mainmenu);
	    		//toggleWindow(tutorial);
	    	}
	    	if(v==mainmenu_btn_quit){
	    		//toggleWindow(mainmenu);
	    		finish();
	    	}	
	    }
	};


	//CREATE PAUSE MENU
	public void createPauseMenu(){
		pausemenu = new PopupWindow(View.inflate(this, R.layout.pausemenu, null), LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, false);
	    //mainmenu.setAnimationStyle(R.style.mainmenu_anim);
	    
		
		//buttons here
		pausemenu_btn_resume = (Button)pausemenu.getContentView().findViewById(R.id.pausemenu_btn_resume);
		pausemenu_btn_resume.setOnClickListener(pausemenuListener);
	    pausemenu_btn_mainmenu = (Button)pausemenu.getContentView().findViewById(R.id.pausemenu_btn_mainmenu);
	    pausemenu_btn_mainmenu.setOnClickListener(pausemenuListener);
	    pausemenu_btn_quit = (Button)pausemenu.getContentView().findViewById(R.id.pausemenu_btn_quit);   
	    pausemenu_btn_quit.setOnClickListener(pausemenuListener);
	    
	}
	//PAUSE MENU CLICK LISTENERS
	private OnClickListener pausemenuListener = new OnClickListener() {
	    public void onClick(View v) {
	    	if(v==pausemenu_btn_resume){
	    		toggleWindow(pausemenu);
	    		//toggleWindow(singlemenu);
	    	}
	    	if(v==pausemenu_btn_mainmenu){
	    		toggleWindow(pausemenu);
	    		//toggleWindow(multimenu);
	    	}
	    	if(v==pausemenu_btn_quit){
	    		//toggleWindow(mainmenu);
	    		finish();
	    	}	
	    }
	};


	public boolean onKeyDown(int keyCode, KeyEvent event) { 
	     // TODO Auto-generated method stub 
	                
	     switch (keyCode) { 
	     case KeyEvent.KEYCODE_MENU: 
	           
	          toggleWindow(pausemenu); 
	          return true; 
	     } 
	      
	     return false; 
	}
    
    
    public void toggleWindow(PopupWindow win){  	
    	if(!win.isShowing()){
    		win.showAtLocation(this.findViewById(R.id.main_layout), Gravity.CENTER, 0, 0);
    	}
    	else{
    		win.dismiss();
    	}
    }


}